<?php


namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 */
class Users extends Model
{
    public $timestamps = false;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'users';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    protected $hidden=[
        'password',
        'pay_password',
        'login_password',
        'openid',
        'user_money',
        'frozen_user_money',
        'live_money',
        'frozen_live_money'
    ];

    /**
     * 资金变更
     * @param [type] $name  // 类型  如：订单支付  资金冻结
     * @param [type] $type  // 修改字段 如：money  freeze_money  integral
     * @param [type] $money  // 金额 10   -10
     * @param array $user_info // 用户信息
     * @param string $info // 描述
     * @param string $pay_type // 支付类型  余额支付
     * @param int $buy_user_id // 邀请人用户id
     * @param int $money_type // 账户类型;2:网红;1:事业;0:店铺;
     * @param string $make_rand // 订单id;
     * @return bool
     */
    public function money_change(
        $name,
        $type,
        $money,
        $user_info = [],
        $info = '',
        $pay_type = '余额支付',
        $buy_user_id = 0,
        $money_type = MoneyLog::MONEY_TYPE_STORE,
        $make_rand = null
    )
    {
        if ($money == 0) {
            return false;
        }
        $money_log_model = new MoneyLog();

        // 资金变更日志
        $log_data = [
            'log_no' => $make_rand,
            'user_id' => $user_info['id'],
            'nickname' => $user_info['nickname'],
            'name' => $name,
            'money' => $money,
            'pay_type' => $pay_type,
            'info' => $info,
            'buy_user_id' => $buy_user_id,
            'money_type' => $money_type,
        ];

        if ($money >= 0) {
            $rs = $this->where('id', $user_info['id'])->increment($type, abs($money));
        } else {
            $rs = $this->where('id', $user_info['id'])->decrement($type, abs($money));
        }

        if ($rs) {
            $money_log_model->addLog($log_data); // 插入日志
            return true;
        } else {
            return false;
        }


    }
}